<?php
include_once(dirname(__FILE__) . "/../../configure/configure.php");		//--> global var
include_once(dirname(__FILE__) . "/../configure/admin.config.inc.php");	//--> admin global var
include_once(dirname(__FILE__) . "/../tools/smarty/libs/Smarty.class.php");					//--> out template
include_once("constant/GlobalConstant.php");

include_once("apis/GoogleAffiliateApi.php");
include_once("impl/CategoryImpl.php");

include_once("impl/FeedImpl.php");
include_once("impl/AdvertiserImpl.php");

require_once(dirname(__FILE__) . "/../controlHeader.php");					//--> system control header
require_once(dirname(__FILE__) . "/../controlOperate.php");				//-->

$couponImpl	= new CouponImpl(DB_TAG_COUPON);			//--> Coupon
$merImpl 	= new MerchantImpl(DB_TAG_COUPON);			//--> Merchant
$catImpl 	= new CategoryImpl(DB_TAG_COUPON);			//--> 
$feedImpl	= new FeedImpl(DB_TAG_COUPON, DB_TAG_FEED);

$error_message 	= '';
$ganLinks		= array();

if(isset($_GET['action'])){
	if($_GET['action'] == 'add'){
		$keyword 	= $_GET['keyword'];
		$linkId 	= $_GET['linkId'];
		$merId 		= $_GET['merId'];
		$catId 		= $_GET['catId'];
				
		$oGanLink = $feedImpl->getGanCouponFeed($linkId);
		
		if(is_object($oGanLink) && is_numeric($catId)){
			$couponId = $feedImpl->addGanCouponLinkToCoupon($oGanLink, $merId, $catId);
		}
	}else if($_GET['action'] == 'recommend'){
		$keyword 		= $_GET['keyword'];
		
		$couponId 		= $_GET['couponId'];
		$linkId 		= $_GET['linkId'];
		
		$isRecommend 	= $_GET['isRecommend'];
		
		$couponImpl->setRecommend($couponId, $isRecommend);
		$feedImpl->setRecommend($linkId, $isRecommend);
	}
}

$linkCount = 0;
if(isset($_GET['keyword']) && !empty($_GET['keyword'])){
	$keyword 		= $_GET['keyword'];
	$searchType 	= $_GET['searchType'];
	$createDateMin 	= $_GET['createDateMin'];
	
	
	if(empty($keyword)){
		$error_message	= 'Keyword is required.';
	}else{
		if($searchType == 'advertiserId'){
			$advertiserId = $keyword;
			
			$oMer = $merImpl->getByAdvertiserId(GlobalConstant::SOURCE_ID_GOOGLE, $advertiserId);
		}
		
		if(isset($_GET['SubmitGanApi'])){			
			$ga = new GoogleAffiliateApi();
			$ga->init();
			
			if(!empty($createDateMin))
				$createDateMin	.= ' 01:01:01';
		
			$links = $ga->getLinks(100, 100, $createDateMin, "", 'approved', $keyword, '');
			
//			echo " min create date: " . $createDateMin . '<br>';
//			echo " link count: " . count($links) . '<br>';
			
			$linkCount = count($links);
			
			$feedImpl->addGanCouponFeeds($links, $startTime);			
		}else if(isset($_GET['displayDBgan'])){
			if($searchType == 'linkId'){
				$linkId = $keyword;
				
				$oGanFeed = $feedImpl->getGanCouponFeed($linkId);
				
				if(is_object($oGanFeed)){
					$advertiserId = $oGanFeed->advertiserId;
					
					$oMer = $merImpl->getByAdvertiserId(GlobalConstant::SOURCE_ID_GOOGLE, $advertiserId);
				}
			}
			
		
		}else if(isset($_GET['addAdvertiser'])){
			if($searchType == 'advertiserId'){
				$advertiserId = $keyword;
				
				$ga = new GoogleAffiliateApi();
				$ga->init();
		
				$adv = $ga->getAdvertiser($advertiserId);
				
//				echo "advertiser id: " . $adv->id ."<br>";
				
				if(is_object($adv)){
					$adImpl = new AdvertiserImpl(DB_TAG_COUPON);
										
					$oMer = $adImpl->addMerchantByGooglAdvertiser($adv);
					
					if(is_object($oMer)){
						echo "add merchant success. <br>";
					}else{
						echo "add merchant fail. <br>";
					}
				}
			}
		}
	}
}

if(is_object($oMer)){
	$arrCategories = $merImpl->getCategoryHash($oMer->getId());
	
	$advertiserId = $merImpl->getAdvertiserId($oMer->getId());
	
	if(!empty($advertiserId))
		$where = " WHERE advertiserId = $advertiserId";
	
	if(!empty($linkId)){
		if(!empty($where))
			$where .= " AND linkId = $linkId";
		else
			$where = " WHERE linkId = $linkId";
			
	}
	
	$order = ' ORDER BY r_recommend DESC, createDate DESC';
}else{
	$oMer = new MerchantDomain();
	
	if($searchType == 'advertiserId')
		$error_message	= "not found matched merchant for advertiser id: $keyword";
	
	if(!empty($linkId))
		$where = " WHERE linkId = $linkId";
	else
		$where = '';
		
	$order = ' ORDER BY r_recommend DESC, createDate DESC';
}

$ganLinks = $feedImpl->getGanCouponFeedCustoms($where, $order, $page, DISPLAY_DATA_SIZE);

foreach($ganLinks as $oGanLink){
	$oCoupon = $couponImpl->getByTrackingURL($oGanLink->clickTrackingUrl);
	
	if(is_object($oCoupon)){
		$catId = $couponImpl->getCategoryId($oCoupon->getId());
		
		$oGanLink->r_catId		= $catId;
		
		$oGanLink->r_matched 		= GlobalConstant::MATCH_COUPON_TRACKING_URL;
		$oGanLink->r_recommend	= $oCoupon->getIsRecommend();
		$oGanLink->r_couponId 	= $oCoupon->getId();
	}else{
		if(is_object($oMer)){
			$oCoupon = $feedImpl->getGanCouponIdKeyExisted($oMer->getId(), $oGanLink);
			
			if(is_object($oCoupon)){
				$catId = $couponImpl->getCategoryId($oCoupon->getId());
		
				$oGanLink->r_catId		= $catId;
				
				$oGanLink->r_matched 		= GlobalConstant::MATCH_COUPON_ID_KEY;
				$oGanLink->r_recommend		= $oCoupon->getIsRecommend();
				$oGanLink->r_couponId 		= $oCoupon->getId();
			}
		}else
			$oGanLink->r_couponId = 0;
	}
}

$arrSearchType = array('advertiserId' => 'Advertiser Id', 'linkId' => 'Link Id');


if($linkCount < 1)
	$linkCount = $feedImpl->getTotal();

/*----- out html -----*/
$smarty = new Smarty();					//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;

$smarty->assign('error_message', $error_message);
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('arrSearchType', $arrSearchType);
$smarty->assign('searchTypeS', $searchType);
$smarty->assign('keyword', $keyword);
$smarty->assign('advertiserId', $advertiserId);


$smarty->assign('createDateMin', $createDateMin);


$smarty->assign('selfFileName', $selfFileName);
$smarty->assign('arrOperateInfo', $arrOperateInfo);

$smarty->assign('ganLinks', $ganLinks);
$smarty->assign('linkCount', $linkCount);

$smarty->assign('oMer', $oMer);
$smarty->assign('arrCategories', $arrCategories);

$smarty->assign('PAGE_BAR', $feedImpl->pagenav);

$smarty->display('listFeedCoupon.htm');
?>
